package queen;

import java.util.ArrayList;
import java.util.List;

import com.splashcard.cardsFace.SplashCard;
import com.splashcard.engine.SplashEngine;
import com.splashcard.message.GroupExecCallBackMessage;
import com.splashcard.message.ParaMessage;
import com.splashcard.message.ResultMessage;
import com.splashcard.message.SplashCardMessage;
import com.splashcard.tool.util.EngineKernel;
import com.splashcard.tool.util.EngineKernel.KernelFinishCallBack;



public class QueenCard implements SplashCard,java.io.Serializable {
	private int mapx = 7;
	private int mapy = 7;
	private ResultMessage result;

	public QueenCard(int mapx,int mapy){
		this.mapx = mapx;
		this.mapy = mapy;
	}
	
	@Override
	public void OnCardStart(Thread thread, ParaMessage paras) {
		// TODO Auto-generated method stub
		result = new ResultMessage();
	}

	@Override
	public void OnCardExecue(Thread thread, ParaMessage paras) {
		// TODO Auto-generated method stub
		for(int i=0;i<mapx;i++){
			append(i, 0, null);
		}
	}

	@Override
	public void OnCardFinish(Thread thread, ParaMessage paras) {
		// TODO Auto-generated method stub

	}

	@Override
	public ResultMessage getResult() {
		// TODO Auto-generated method stub
		return result;
	}

	public void append(int x, int y, int[] xs) {
		if (check(x, y, xs)) {
			y++;
			if (y == mapy) {
				xs = appendx(xs,x);
				String RStr = "";
				for (int i = 0; i < xs.length; i++) {
					RStr += "(" + xs[i] + "," + i + ")";
				}
				return;
			} else {
				for (int i = 0; i < mapx; i++) {
					int[] nXs = appendx(xs,x);
					append(i, y, nXs);
				}
			}
		} else {
			return;
		}
	}
	
	public int[] appendx(int[] xs,int target){
		if(xs ==null){
			xs = new int[0];
		}
		
		int[] nXs = new int[xs.length+1];
		for (int k = 0; k < nXs.length; k++) {
			if (k != nXs.length - 1) {
				nXs[k] = xs[k];
			} else {
				nXs[k] = target;
			}
		}
		return nXs;
	}

	public boolean check(int x, int y, int[] xs) {
		if(y==0){
			return true;
		}
		for (int i = 0; i < xs.length; i++) {
			int checkx = xs[i];
			int checky = i;
			if (x == checkx) {
				return false;
			}
			if (Math.abs(x - checkx) == Math.abs(y - checky)) {
				return false;
			}
		}
		return true;
	}

	public static void main(String[] args) {
		
		List<SplashCard> cards = new ArrayList<SplashCard>();
		for(int i=0;i<30;i++){
			cards.add(new QueenCard(12,12));
		}
		
		for(int i=0;i<100;i++){
			cards.add(new QueenCard(11,11));
		}
		
		cards.add(new QueenCard(13,13));
		cards.add(new QueenCard(14,14));
		cards.add(new QueenCard(13,13));
		cards.add(new QueenCard(14,14));
		
		SplashEngine.getInstance().exec(cards, new SplashEngine.OnGroupExecFinish() {
			
			@Override
			public void OnFinish(GroupExecCallBackMessage result) {
				// TODO Auto-generated method stub
				System.out.println("callBack:"+result.getUsedTime()+" avg:"+result.getAvgUsedTime()+" threads:"+result.getThreads());
				System.out.println(" "+result.toString());
			}
		});
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "Name:"+this;
	}

	@Override
	public String getCardDetail() {
		// TODO Auto-generated method stub
		return "mapx:"+mapx+"  mapy:"+mapy;
	}
}
